<?php
// +----------------------------------------------------------------------
// | Author: sam
// | 短信接口
// +----------------------------------------------------------------------
namespace Sam\SMS;

use Sam\ISMS;

ini_set("display_errors", "on");
require_once __DIR__ . '/ali/api_sdk/vendor/autoload.php';

use Aliyun\Core\Config;
use Aliyun\Core\Profile\DefaultProfile;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;
use Aliyun\Api\Sms\Request\V20170525\SendBatchSmsRequest;
use Aliyun\Api\Sms\Request\V20170525\QuerySendDetailsRequest;


class ali implements ISMS
{
    private $config;
    private $acsClient;

    public function __construct()
    {
        self::init();
    }

    public function init()
    {
        Config::load();
        $this->config = include __DIR__ . '/ali/config.php';
        $this->acsClient = $this->getAcsClient();
    }

    public function getAcsClient()
    {
        //产品名称:云通信流量服务API产品,开发者无需替换
        $product = "Dysmsapi";
        //产品域名,开发者无需替换
        $domain = "dysmsapi.aliyuncs.com";
        // TODO 此处需要替换成开发者自己的AK (https://ak-console.aliyun.com/)
        $accessKeyId = $this->config['accessKeyId'] ?? "yourAccessKeyId"; // AccessKeyId
        $accessKeySecret = $this->config['accessKeySecret'] ?? "yourAccessKeySecret"; // AccessKeySecret
        // 暂时不支持多Region
        $region = "cn-hangzhou";
        // 服务结点
        $endPointName = "cn-hangzhou";
        if ($this->acsClient == null) {
            //初始化acsClient,暂不支持region化
            $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
            // 增加服务结点
            DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);
            // 初始化AcsClient用于发起请求
            $this->acsClient = new DefaultAcsClient($profile);
        }
        return $this->acsClient;
    }



    private function SMSsend($phone, $message)
    {

        // 初始化SendSmsRequest实例用于设置发送短信的参数
        $request = new SendSmsRequest();
        //可选-启用https协议
        //$request->setProtocol("https");
        // 必填，设置短信接收号码
        $request->setPhoneNumbers((string)$phone);
        // 必填，设置签名名称，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
        $request->setSignName($this->config['signName']);
        // 必填，设置模板CODE，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
        $request->setTemplateCode($this->config['templateCode']);
        // 可选，设置模板参数, 假如模板中存在变量需要替换则为必填项
        $request->setTemplateParam(json_encode(array(  // 短信模板中字段的值
            "code" => $message,
        ), JSON_UNESCAPED_UNICODE));
        // 可选，设置流水号
        $request->setOutId("yourOutId");
        // 选填，上行短信扩展码（扩展码字段控制在7位或以下，无特殊需求用户请忽略此字段）
        $request->setSmsUpExtendCode("1234567");
        // 发起访问请求
        $acsResponse = $this->getAcsClient()->getAcsResponse($request);
        return $acsResponse;
    }


    public function send($phone, $message)
    {
        if (!$phone || !$message) {
            return false;
        }
		if (!in_array($phone,$this->config['white'])){
			return false;
		}
        try {
            $status = $this->SMSsend($phone, $message);
        } catch (\Exception $e) {
            return false;
        }
        return $status;
    }
}

